using System;
using System.Collections;

namespace codebase.UI
{
    public class WindowManager
    {
        ArrayList Windows = new ArrayList();

#if NDS
        public NDSTouchscreen touchscreen;
#endif

        public UI.Renderer screen;
        public Window inFront = null;

#if NDS
        public WindowManager(UI.Renderer gfx, NDSTouchscreen touch)
#else
        public WindowManager(UI.Renderer gfx)
#endif
        {
            screen = gfx;

#if !PC
#if NDS
            touchscreen = touch;

            touchscreen.TouchDownEvent += new TouchDownHandler(touchscreen_TouchDownEvent);
            touchscreen.TouchMoveEvent += new TouchMoveHandler(touchscreen_TouchMoveEvent);
            touchscreen.TouchUpEvent += new TouchUpHandler(touchscreen_TouchUpEvent);
#endif

#if PICASO
            /*screen.SPE.TouchDownEvent += new TouchDownHandler(touchscreen_TouchDownEvent);
            screen.SPE.TouchMoveEvent += new TouchMoveHandler(touchscreen_TouchMoveEvent);
            screen.SPE.TouchUpEvent += new TouchUpHandler(touchscreen_TouchUpEvent);*/
#endif
#endif
        }

        public void Add(Window window)
        {
            if (window != null)
            {
                Windows.Add(window);
            }
        }

        public void Remove(Window window)
        {
            if (window != null)
            {
                Windows.Remove(window);
            }
        }

        public void Redraw()
        {
            foreach (object o in Windows)
            {
                Window w = (Window)o;
                w.Redraw();
            }
        }

#if !PC
        void touchscreen_TouchDownEvent(object o, TouchEventArgs e)
        {
            foreach (object ob in Windows)
            {
                Window w = (Window)ob;
                if (inFront != null)
                {
                    if (inFront != w) continue;
                }
                w.touchscreen_TouchDownEvent(o, e);
            }
        }

        void touchscreen_TouchMoveEvent(object o, TouchEventArgs e)
        {
            foreach (object ob in Windows)
            {
                Window w = (Window)ob;
                if (inFront != null)
                {
                    if (inFront != w) continue;
                }
                w.touchscreen_TouchMoveEvent(o, e);
            }
        }

        void touchscreen_TouchUpEvent(object o, TouchEventArgs e)
        {
            foreach (object ob in Windows)
            {
                Window w = (Window)ob;
                if (inFront != null)
                {
                    if (inFront != w) continue;
                }
                w.touchscreen_TouchUpEvent(o, e);
            }
        }

#endif
    }
}
